<template>
  <div>
    <div class="filter-container mt-15">
      <el-button v-if="checkPermission(permissionRoles.reason_config_download)" v-waves :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
      <el-upload
        v-if="checkPermission(permissionRoles.reason_config_upload)"
        ref="upload"
        v-waves
        class="filter-item"
        action="api/gm/behavior/reasonconfig/upload"
        :headers="header"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="successInfo"
        :limit="1"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button type="success" icon="el-icon-upload2">上传</el-button>
      </el-upload>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="680">
      <el-table-column label="原因ID" prop="id" align="center"></el-table-column>
      <el-table-column label="原因" prop="name" align="center"></el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination'
import checkPermission from '@/utils/permission'
import { getReasonlConfig } from '@/api/actorInfo'

export default {
  name: 'BehaviorLog',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      header: {
        Authorization: this.$store.state.user.token
      },
      total: 0,
      list: [],
      listLoading: false,
      downloadLoading: false,
      listQuery: {
        page: 1,
        limit: 200
      },
      fileList: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    checkPermission,
    init() {
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getReasonlConfig()
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['原因ID', '原因']
        const filterVal = ['id', 'name']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '原因配置'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    successInfo(res, file, fileList) {
      this.init()
      if (res.code === 40003) {
        this.$message.error(res.message)
      } else {
        this.$refs.upload.clearFiles()
        this.visible = false
        this.$message.success(res.message)
      }
    }
  }
}
</script>
